package com.crm.business.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.crm.business.admin.mapper.AdminMenuMapper;
import com.crm.business.admin.service.IAdminMenuService;
import com.crm.common.admin.common.AdminRoleTypeEnum;
import com.crm.common.admin.entity.PO.AdminMenu;
import com.crm.common.admin.entity.VO.AdminMenuVO;
import com.crm.business.servlet.BaseServiceImpl;
import com.crm.common.ka.core.common.cache.AdminCacheKey;
import com.crm.common.ka.core.redis.Redis;
import com.crm.common.ka.core.utils.RecursionUtil;
import com.crm.business.utils.UserUtil;
import com.crm.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 后台菜单表 服务实现类
 * </p>
 *
 * @author zhangzhiwei
 * @since 2020-04-27
 */
@Service
public class AdminMenuServiceImpl extends BaseServiceImpl<AdminMenuMapper, AdminMenu> implements IAdminMenuService {

    @Autowired
    private Redis redis;


    /**
     * 查询用户所拥有的菜单权限
     *
     * @param userId 用户列表
     * @return 菜单权限的并集
     */
    @Override
    public List<AdminMenu> queryMenuList(Long userId) {
        if (UserUtil.isAdmin()) {
            return query().list();
        }
        return getBaseMapper().queryMenuList(userId);
    }

    /**
     * 查询公海菜单权限
     *
     * @param userId 用户ID
     * @param deptId 部门ID
     * @return data
     */
    @Override
    public Map<String, Long> queryPoolReadAuth(Long userId, Integer deptId) {
        return getBaseMapper().queryPoolReadAuth(userId, deptId);
    }

    /**
     * 根据类型查询菜单
     * @return data
     */
    @Override
    public JSONObject getMenuListByType() {
        JSONObject object = shwoAllMemu();
        return object;
    }


    /**
     * 展示所有menu
     * @return data
     */
    @Override
    public JSONObject shwoAllMemu() {
        JSONObject object = new JSONObject();
        List<AdminMenuVO> result = new ArrayList<>();
        List<AdminMenu> adminMenuList = lambdaQuery().eq(AdminMenu::getParentId, 0).list();
        for(AdminMenu adminMenu : adminMenuList){
            AdminMenuVO data = BeanUtil.copyProperties(adminMenu, AdminMenuVO.class);
            List<AdminMenuVO> menuList = getMenuList(data.getMenuId());
            data.setChildMenu(menuList);
            result.add(data);
        }
        object.put("dataList",result);
        return object;
    }

    /**
     * 通过parentId和realm查询菜单
     *
     * @return 菜单列表
     * @author zhangzhiwei
     */
    private AdminMenuVO queryMenuListByRealm(String realm) {
        AdminMenu adminMenu = lambdaQuery().eq(AdminMenu::getParentId, 0).eq(AdminMenu::getRealm, realm).one();
        return BeanUtil.copyProperties(adminMenu, AdminMenuVO.class);
    }

    private List<AdminMenuVO> getMenuList(Integer parentId, String... notRealm) {
        LambdaQueryChainWrapper<AdminMenu> chainWrapper = lambdaQuery();
        if (notRealm.length > 0) {
            chainWrapper.notIn(AdminMenu::getRealm, Arrays.asList(notRealm));
        }
//        if(!UserUtil.isAdmin()){
//            chainWrapper.eq(AdminMenu::getStatus,1);
//        }
        chainWrapper.orderByAsc(AdminMenu::getSort);
        List<AdminMenu> list = chainWrapper.list();
        return RecursionUtil.getChildListTree(list, "parentId", parentId, "menuId", "childMenu", AdminMenuVO.class);
    }

    @Override
    public Integer queryMenuId(String realm1, String realm2, String realm3) {
        return getBaseMapper().queryMenuId(realm1,realm2,realm3);
    }

    @Override
    public int addMenuList(List<AdminMenu> menuList){
        return getBaseMapper().addMenuList(menuList);
    }

    /**
     * 查询crm业务菜单
     * @return data
     */
    @Override
    public List<AdminMenuVO> queryCrmMenu() {
        List<AdminMenu> firstMenuList = lambdaQuery().eq(AdminMenu::getParentId, 0).eq(AdminMenu::getRealm,"crm").list();
        List<AdminMenuVO> menuList = new ArrayList<>();
        for(AdminMenu firstMenu : firstMenuList){
            LambdaQueryChainWrapper<AdminMenu> chainWrapper = lambdaQuery();
            chainWrapper.eq(AdminMenu::getParentId,firstMenu.getMenuId());
            chainWrapper.orderByAsc(AdminMenu::getSort);
            List<AdminMenu> list = chainWrapper.list();
            List<AdminMenuVO> childMenu = RecursionUtil.getChildListTree(list, "parentId", firstMenu.getMenuId(), "menuId", "childMenu", AdminMenuVO.class);
            menuList.addAll(childMenu);
        }
        return menuList;
    }

    @Override
    public void upMenuStatus(Integer menuId,Integer status) {
        AdminMenu up = new AdminMenu();
        up.setMenuId(menuId);
        up.setStatus(status);
        getBaseMapper().updateById(up);
        String cacheKey = AdminCacheKey.USER_AUTH_CACHE_KET;
        redis.del(cacheKey);
    }
}
